<?php

require_once 'common/controller/CommonBase.php';
require_once 'common/controller/Auth.php';

/**
 *
 * 获取当天时间
 */
function getTodayTime(){

    $data['beginToday'] = mktime(0,0,0,date('m'),date('d'),date('Y'));
    $data['endToday'] = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;

    $beginTime = date('Y-m-d H:i:s',$data['beginToday']);
    $endTime = date('Y-m-d H:i:s',$data['endToday']);
    $where = ['create_at','between',"{$beginTime},{$endTime}"];
    return $where;
}


/**
 *
 * 计算时间
 */
function countTime($time){


    $diffTime = time() - $time;
    if($diffTime < 10){

        $return_time = '刚刚发布';
    }elseif ($diffTime >= 10 && $diffTime < 60){

        $return_time = $diffTime.'秒前发布';
    }elseif ($diffTime >= 60 && $diffTime < 3600){

        $return_time = floor($diffTime / 60).'分钟前发布';
    }elseif ($diffTime >= 3600 && $diffTime < (3600 * 24)){

        $return_time = floor($diffTime / 3600).'小时前发布';
    }elseif ($diffTime >= (3600 * 24) && $diffTime < (3600 * 24 * 30)){

        $return_time = floor($diffTime / 3600  / 24).'天前发布';
    }elseif ($diffTime >= (3600 * 24 * 30) && $diffTime < (3600 * 24 * 30 * 12)){

        $return_time = floor($diffTime / 3600  / 24 / 30).'月前发布';
    }else{

        $return_time = floor($diffTime / 3600  / 24 / 30 / 12).'年前发布';
    }

    return $return_time;


}

/**
 *
 * 分页
 */
function page($params){

    #分页
    $page_no = isset($params['page_no']) && !empty($params['page_no']) ?$params['page_no']:1; // 页码
    $page_size = isset($params['page_size']) && !empty($params['page_size']) ?$params['page_size']:10; //多少条数据
    if ($page_no <= 0) $page_no = 1;
    $page = ($page_no - 1) * $page_size;
    if ($page < 0) $page = 0;

    $data['page'] = $page;
    $data['page_size'] = $page_size;
    return $data;
}

//curl post 请求
function postJsonCurl($data,$url,$type=1)
{
    $headers = array("Content-type: application/json","Accept: application/json");
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60); //设置超时
//        if(0 === strpos(strtolower($url), 'https')) {
//            　　curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //对认证证书来源的检查
//            　　curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //从证书中检查SSL加密算法是否存在
//        }
    if($type){
        curl_setopt($ch, CURLOPT_POST, TRUE);
        $res_json=json_encode($data);
        $res_json = str_replace("\\/", "/", $res_json);//halt($res_json);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $res_json);
    }else{
        curl_setopt($ch, CURLOPT_POST, false);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $rtn = curl_exec($ch);//CURLOPT_RETURNTRANSFER 不设置  curl_exec返回TRUE 设置  curl_exec返回json(此处) 失败都返回FALSE
    curl_close($ch);
    return $rtn;
}

function post($curlPost, $url, $ssl = false)
{
    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_NOBODY, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
    if (!$ssl) {
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    }
    $return_str = curl_exec($curl);
    curl_close($curl);
    return $return_str;
}

/**
* 过滤emoji函数
*/
function removeEmoji($text){
    $len = mb_strlen($text);
    $newText = '';
    for($i=0;$i<$len;$i++){
        $str = mb_substr($text, $i, 1, 'utf-8');
        if(strlen($str) >= 4) continue;//emoji表情为4个字节
        $newText .= $str;
    }
    return $newText;
}

/**
 *
 * 根据生日获取年龄
 */
function ageSum($time){
    $value = [
        "years" => '', "mou" => '',"days" => '1',
    ];
    if ($time >= 31556926) {
        $value["years"] = floor($time/31556926). "岁";
        $time = ($time%31556926);
    }
    if ($time >= 2626560){
        $value["mou"] = floor($time/2626560)."月";
        $time = ($time%2626560);
    }
    if ($time >= 86400) {
        $value["days"] = floor($time/86400);
        $time = ($time%86400);
        if ($time>=0){
            $value['days']++;
        }
    }
    $value['days'] = $value['days']."天";
    return implode($value);
}

/**
 *
 * 获取本周、本月开始时间
 */
function getTime($type = 0){

    if ($type == 0){

        //当前日期
        $today = date("Y-m-d");
//$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
        $first=1;
//获取当前周的第几天 周日是 0 周一到周六是 1 - 6
        $w = date('w',strtotime($today));
//获取本周开始日期，如果$w是0，则表示周日，减去 6 天
        $data['start_at'] = date('Y-m-d H:i:s',strtotime("$today -".($w ? $w - $first : 6).' days'));
//本周结束日期
        $data['end_at'] = date('Y-m-d  H:i:s',strtotime("{$data['start_at']} +6 days"));
    }elseif ($type == 1){

        $start_time = strtotime(date('Y-m-01 00:00:00'));
        $end_time  = strtotime(date('Y-m-t 23:59:59'));
       $data['start_at'] = date('Y-m-d H:i:s',$start_time);
       $data['end_at'] = date('Y-m-d H:i:s',$end_time);
    }else{

        $data['start_at'] = date('Y-m-d H:i:s',strtotime(date("Y",time())."-1"."-1"));
        $data['end_at'] = date('Y-m-d H:i:s',strtotime(date("Y",time())."-12"."-31 23:59:59"));
    }

    return $data;
}

/**
 *
 * 时间间隔
 */
function timeInterval($time_at,$type = 'time_at'){

    $time_interval = strtotime($time_at[1]) - strtotime($time_at[0]);
    $data = [];

    if (date('Y-m',strtotime($time_at[0])) == date('Y-m',strtotime($time_at[1])) || $type == 'week' || $type == 'month'){

        $time = $time_interval/86400;
        for ($i=0;$i <= $time;$i++){

            $data[] = date('Y-m-d',strtotime($time_at[0])+($i*86400));
        }
    }else{
        $time1 = strtotime(date('Y-m-1',strtotime($time_at[0])));
        $time2 = strtotime(date('Y-m-t',strtotime($time_at[1])));
        $time_interval = $time2 - $time1;
        $time = round($time_interval/2592000,0);
        for ($i=1;$i <= $time;$i++){

            if ($i < 10){

                $data[] = date('Y-'.'0'.$i);
            }else{

                $data[] = date('Y-'.$i);
            }


        }

    }
    return $data;
}

/**
 *
 * 更加时间戳获取年月
 */
function yearMonth($date1 = '',$date2 = ''){

    if (strtotime($date1) > strtotime($date2)) {
        $ymd = $date2;
        $date2 = $date1;
        $date1 = $ymd;
    }
    list($y1, $m1, $d1) = explode('-', $date1);
    list($y2, $m2, $d2) = explode('-', $date2);
    $y = $m = $d = $_m = 0;
    $math = ($y2 - $y1) * 12 + $m2 - $m1; //相差总月数
    $y = round($math / 12);  //相差年数
    $m = intval($math % 12);  //相差月数
    $mm = $math; //相差总月数
    $d = (mktime(0, 0, 0, $m2, $d2, $y2) - mktime(0, 0, 0, $m2, $d1, $y2)) / 86400;
    if ($d < 0) {
        $m -= 1;
        $d += date('j', mktime(.0, 0, 0, $m2, 0, $y2));
    }
    $m < 0 && $y -= 1;
    return $y.'岁'.$m.'个月';


}


function web_is_open()
{

    //1.获取当前站点的状态
    $isOpen = \think\Db::table('system_config')->where([
        ['name','eq','is_open']
    ])->value('value');
    //2.如果站点是关闭状态,那我们只允许关闭前台模块,后台模块必须仍然可以访问
    if ($isOpen==0) {
        //或者写上:此域名出售
        $info = <<<'INFO'
            <body style="background-color:#333">
            <h1 style="color:#eee;text-align:center;margin:200px">站点维护中...</h1>
            </body>
INFO;
        exit($info);
    }
}

/**
 * Json Decode 安全的解析
 *
 * @since v1.0.0
 * @author ranj
 * @param string|array $data
 * @param bool $assoc
 * @return array|object
 */
function maybe_json_decode($data, $assoc = true)
{
    if (!$data) {
        return $assoc ? array() : new stdClass();
    }

    if (is_array($data) || is_object($data)) {
        return $data;
    }

    if (is_string($data)) {
        $result = json_decode($data, $assoc);
        if (!$result) {
            return $assoc ? array() : new stdClass();
        }
        return $result;
    }

    return array();
}

/**
 * json_encode
 *
 * @param $data
 * @return false|string
 */
function maybe_json_encode($data)
{
    if (array_is($data)) {
        return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
    }

    if (!$data) {
        return json_encode(array(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);;
    }

    if (is_string($data)) {
        return $data;
    }

    return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
}


/**
 * 判断是否为array 数据
 *
 * @since v1.0.0
 * @author ranj
 * @param array $array
 * @return bool
 */
function array_is($array=array()){
    if(!$array||!is_array($array)){
        return false;
    }
    return true;
}


/**
 * 判断是否是空字符串
 *
 * @since v1.0.0
 * @author ranj
 * @param string $string
 * @return bool
 */
function is_null_or_empty($string)
{
    if (is_null($string)) {
        return true;
    }

    if (is_array($string) && !count($string)) {
        return true;
    }

    if (!is_string($string) && $string) {
        return false;
    }
    return is_null($string) || $string === '';
}
/**
 * 二维数组根据某个字段去重
 * @param array $array  二维数组
 * @para array  去重后的数组
 */
 function uniquArr($array){
    $result = array();
    foreach($array as $k=>$val){
        $code = false;
        foreach($result as $_val){
            if($_val['part_name'] == $val['part_name']){
                $code = true;
                break;
            }
        }
        if(!$code){
            $result[]=$val;
        }
    }
    return $result;
}